<?php
require_once dirname(__FILE__) . '/metadata.php';
require_once dirname(__FILE__) . '/EmailFetcher.class.php';
require_once dirname(__FILE__) . '/DbUtils.class.php';
require_once dirname(__FILE__) . '/includes/MiscUtils.class.php';

$emails = array();
$mailHeader = array();
$mailAttach = array();
$mailBody = '';
$mail = new EmailFetcher();
//$server_path = '/home/plateau/dreamlife.co.nz/pz/enqsys/attaches/';
//$connect = $mail->mailConnect('imap.webhost.co.nz', 993, 'enquiry@dreamlife.co.nz', 'dreamlife1234', 'INBOX', 'ssl');
$connect = $mail->mailConnect('imap.gmail.com', 993, 'nzios.atdn@gmail.com', 'nzios123456', 'INBOX', 'ssl');
$server_path = dirname(__FILE__) . '/ext/plugin/bin/attendences/uploads/';
if($connect) {
  for ($mailId = 0, $totalCount = $mail->mailTotalCount(); $mailId < $totalCount; $mailId++) {
    $mailAttach = $mail->getAttach($mailId + 1, $server_path);
    foreach ($mailAttach as $attach) {
      $path = $server_path . $attach[0];
      upload($path,$attach);
    }
    $mail->mailDelete($mailId + 1);
  }
  $mail->closeMail();
}

function upload($path,$attach){
  $myPdo = DbUtils::createPdoInst();

  $result = array();
  $sessions = array();

  date_default_timezone_set('Pacific/Auckland');
  require_once dirname(__FILE__) . '/libs/phpexcel/PHPExcel/IOFactory.php';
  $objPHPExcel = PHPExcel_IOFactory::load($path);

  $header = json_decode($objPHPExcel->getProperties()->getDescription());
  if (!$header || !isset($header->secret) || $header->secret != SECRET) {
    return;
  }

  $jl = $header->sessions;
  for ($j = 0; $j < $jl; $j++) {
    $tmp = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($j + 2, 2)->getValue();
    if (!$tmp) {
      break;
    }
    $tmp2 = split('#', $tmp);
    $sessions[] = array('id' => $tmp2[0], 'date' => $tmp2[1]);
  }

  $il = $header->students;
  for ($i = 0; $i < $il; $i++) {
    $tmp = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, $i + 4)->getValue();
    $tmp2 = split('#', $tmp);
    $student_id = $tmp2[0];

    $enrolled_program_id = 0;
    $enrolled_semester_id = 0;
    $cond_vals = new stdClass();
    $cond_vals->c = 't.student_id = :v1 AND t.class_id = :v2';
    $cond_vals->v = array(':v1' => $student_id, ':v2' => $header->class_id);
    $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($tmp) > 0) {
      $enrolled_program_id = $tmp[0]->enrolled_program_id;
      $enrolled_semester_id = $tmp[0]->enrolled_semester_id;
    }
    
    for ($j = 0; $j < $jl; $j++) {
      $tmp = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($j + 2, $i + 4)->getValue();
      $obj = new stdClass();
      $obj->track_id = MiscUtils::unique();
      $obj->session_id = $sessions[$j]['id'];
      $obj->date = $sessions[$j]['date'];
      $obj->class_id = $header->class_id;
      $obj->enrolled_program_id = $enrolled_program_id;
      $obj->enrolled_semester_id = $enrolled_semester_id;
      $obj->student_id = $student_id;
      $obj->is_absent = 0;
      $obj->is_late = 0;
      $obj->is_tutorial = 0;
      $obj->week_num = 0;
      $obj->time = '00:00:00';
      if (strtolower($tmp) == 'a') {
        $obj->is_absent = 1;
      }
      if (strtolower($tmp) == 'l') {
        $obj->is_late = 1;
      }
      $result[] = $obj;
    }
  }
  $cond_vals = new stdClass();
  $cond_vals->c = 'date >= :v1 AND date <= :v2 AND class_id = :v3';
  $cond_vals->v = array(':v1' => $header->start, ':v2' => $header->end, ':v3' => $header->class_id);
  DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals);
    
  foreach ($result as $tmp) {
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $tmp);
  }

  $obj = new stdClass();
  $obj->track_id = MiscUtils::unique();
  $obj->class_id = $header->class_id;
  $obj->start = $header->start;
  $obj->end = $header->end;
  $obj->date = date('Y-m-d', time());
  $obj->text = '';
  $obj->is_uploaded = 1;
  $obj->file = $attach[1];
  $obj->path = basename($path);
  DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_logs', $obj);
}
?>
